﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using HotKey.Core;
using SendKeyTo.Core;
using WKeys = System.Windows.Forms.Keys;
using AMing.Windows.Net35;

namespace SendKeyTo
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            InitControls();


            this.Loaded += MainWindow_Loaded;
            this.StateChanged += MainWindow_StateChanged;
        }

        void MainWindow_StateChanged(object sender, EventArgs e)
        {
            //if (this.WindowState == System.Windows.WindowState.Minimized)
            //{
            //    this.Hide();
            //}
            //else
            //{
            //    this.Show();
            //}
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            RegisterHotKey();
            this.Hide();
        }
        Hardcodet.Wpf.TaskbarNotification.TaskbarIcon taskbarIcon = new Hardcodet.Wpf.TaskbarNotification.TaskbarIcon();

        ContextMenu cm_TaskbarIcon = new ContextMenu();

        private void InitControls()
        {
            taskbarIcon.Icon = Resource.Application;
            //taskbarIcon.ShowBalloonTip("Hello", "Hello!!!!", Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info);

            taskbarIcon.ContextMenu = cm_TaskbarIcon;

            MenuItem mi_sh = new MenuItem() { Header = "Show/Hide" };
            mi_sh.Click += mi_sh_Click;
            cm_TaskbarIcon.Items.Add(mi_sh);

            MenuItem mi = new MenuItem() { Header = "Exit" };
            mi.Click += mi_Click;
            cm_TaskbarIcon.Items.Add(mi);

            chb_auto.IsChecked = AutoRun.GetAutoRun(AutoRunKey);

            chb_auto.Checked += chb_auto_Checked;
            chb_auto.Unchecked += chb_auto_Unchecked;
        }

        void chb_auto_Checked(object sender, RoutedEventArgs e)
        {
            string thispath = Process.GetCurrentProcess().MainModule.FileName;
            AutoRun.AddAutoRunkey(thispath, AutoRunKey);
        }

        void chb_auto_Unchecked(object sender, RoutedEventArgs e)
        {
            AutoRun.DelAutoRunkey(AutoRunKey);
        }

        const string AutoRunKey = "SendKeyTo";

        void mi_sh_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Normal)
            {
                this.WindowState = System.Windows.WindowState.Minimized;
                this.Hide();
            }
            else
            {
                this.Show();
                this.WindowState = System.Windows.WindowState.Normal;
            }
        }


        void mi_Click(object sender, RoutedEventArgs e)
        {
            App.Current.Shutdown();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Core.Keybd_Event.KeyDown(Core.Keybd_Enum.vbKeySoundOff);
        }

        private Process GetSai()
        {
            Process[] processes = System.Diagnostics.Process.GetProcesses();
            foreach (var item in processes)
            {
                string name = item.ProcessName;
                if (name.ToLower() == "sai")
                {
                    return item;
                }
            }
            return null;
        }

        private void btn_shan_Click_1(object sender, RoutedEventArgs e)
        {
            KeyPrevious();
        }

        private void btn_xia_Click_1(object sender, RoutedEventArgs e)
        {
            KeyNext();
        }

        private void btn_pp_Click_1(object sender, RoutedEventArgs e)
        {
            KeyPlayOrPause();
        }

        private void btn_so_Click_1(object sender, RoutedEventArgs e)
        {
            KeySoundOff();
        }

        #region MyRegion
        /// <summary>
        /// 注册热键
        /// </summary>
        private void RegisterHotKey()
        {
            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.PageUp, KeyPrevious);
            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.PageDown, KeyNext);
            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.Home, KeyPlayOrPause);

            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.Insert, KeyVolumeUp);
            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.Delete, KeyVolumeDown);
            AddHotKey(HotKey.Core.HotKey.KeyFlags.MOD_Control | HotKey.Core.HotKey.KeyFlags.MOD_Shift, WKeys.End, KeySoundOff);

        }

        private void AddHotKey(HotKey.Core.HotKey.KeyFlags control, WKeys key, HotKey.Core.HotKey.OnHotKeyEventHandler keyDownEvent)
        {
            HotKey.Core.HotKey hotkey = new HotKey.Core.HotKey(this, control, key);
            hotkey.OnHotKey += keyDownEvent;
        }


        #endregion

        #region Event

        private void KeyDown(Keybd_Enum keyenum)
        {
            Core.Keybd_Event.KeyDown(keyenum);
            taskbarIcon.ShowBalloonTip("KeyDown", Keybd_Info.GetKeyInfo(keyenum), Hardcodet.Wpf.TaskbarNotification.BalloonIcon.Info);
        }


        private void KeyPrevious()
        {
            KeyDown(Keybd_Enum.vbKeyPrevious);
        }
        private void KeyNext()
        {
            KeyDown(Keybd_Enum.vbKeyNext);
        }
        private void KeyPlayOrPause()
        {
            KeyDown(Keybd_Enum.vbKeyPlayOrPause);
        }
        private void KeySoundOff()
        {
            KeyDown(Keybd_Enum.vbKeySoundOff);
        }
        private void KeyVolumeUp()
        {
            KeyDown(Keybd_Enum.vbKeyVolumeUp);
        }
        private void KeyVolumeDown()
        {
            KeyDown(Keybd_Enum.vbKeyVolumeDown);
        }

        #endregion
    }
}
